package com.ys.controller.api.storage;

import com.ys.config.MenuId;
import com.ys.entity.storage.TccWarehouseArea;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.storage.TccWarehouseAreaService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 库区管理
 */
@Controller
@RequestMapping("/api/inter/tccWarehouseArea")
public class ApiInterTccWarehouseAreaController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTccWarehouseAreaController.class);

    @Autowired
    private CommonApiService<TccWarehouseArea> commonApiService;
    
    Long menuId = MenuId.TCC_WarehouseArea;
    
    @Autowired
    private TccWarehouseAreaService tccWarehouseAreaService;

    /**
     * 设置查询的cond的sql设置
     */
    private TccWarehouseArea setSeachSql(TccWarehouseArea cond){
       
        return  cond;
    }

    @RequiresPermissions("tccWarehouseArea:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA searchDetail(@ModelAttribute TccWarehouseArea cond){

        cond = setSeachSql(cond);
        cond.setOrderby("A.Id DESC")
        .addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
        .addLeftJoin("TB_Country tb ON tb.code = B.countryCode")
        .addField("B.Vck vck,B.CountryCode countryCode,B.City city,tb.name countryName");
        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }
    /*
    *库区
     */
    @RequestMapping("/search1")
    @ResponseBody
    public LayuiTableVoJA searchDetail1(@ModelAttribute TccWarehouseArea cond){

        cond = setSeachSql(cond);
        cond.setOrderby("A.Id DESC")
                .addLeftJoin("TCC_Warehouse B ON B.Cck=A.Cck")
                .addLeftJoin("TB_Country tb ON tb.code = B.countryCode")
                .addField("B.Vck vck,vck")
                .setStatus(1);
        LayuiTableVoJA vo = commonApiService.searchLay(cond);
        return vo;
    }

    /*
     * 导出
     */
    @RequiresPermissions("tccWarehouseArea:export")
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TccWarehouseArea cond) throws Exception{
        cond.setOrderby("A.Id DESC");

        cond.setPage(0).setSize(999999);

        List<Map<String, Object>> listData = cond.queryMap();

        List<YsCol> listField = new YsCol()
                .setMenuId(menuId)
                .setFrontShow(YsColEnum.FrontShow.SHOW.value)
                .setOrderby("A.SortNo asc")
                .queryList();


        ExportExcelUtils<TccWarehouseArea> util = new ExportExcelUtils<TccWarehouseArea>(new TccWarehouseArea());

        String toFilePath = util.exportExcel(listData, listField, "库区管理");
        UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }
    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("库区新增传参：" + params);
            //修改
            return tccWarehouseAreaService.add(params);
        } catch (Exception e) {
            logger.error("库区新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 编辑
     */
    @PostMapping("/toEdit")
    @ResponseBody
    public R edit(@RequestParam Map<String, Object> params){
        try {
            return tccWarehouseAreaService.edit(params);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
}
